package com.guomao.controller;


import com.guomao.dao.OrderDao;
import com.guomao.entity.Order;
import com.guomao.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.*;

//@Controller

//@RestController
//@Transaction
@Controller
public class OrderController {

    @Autowired
    private OrderDao dao;

    @RequestMapping(value = "/order", method = RequestMethod.GET)
    public String getAllOrder(Model m) {
        Collection<Order> orders = dao.getAll();
        m.addAttribute("orders", orders);
        return "orderList";
    }


    @DeleteMapping(value = "/order/{id}")
    public String deleteOrder(@PathVariable("id") Integer id) {
        dao.deleteById(id);
        return "redirect:/order";
    }


    //    @RequestMapping(value = "/order", method = RequestMethod.POST)
    @PostMapping(value = "/order")
    public String saveOrder(Order order) {
        dao.save(order);
        return "redirect:/order";
    }


    @RequestMapping(value = "/getOrder/{id}", method = RequestMethod.GET)
    public String getOrder(@PathVariable("id") Integer id, Model m) {
        Order order = dao.getById(id);
        m.addAttribute("order", order);
        return "orderUpdate";
    }


    @RequestMapping(value = "/order", method = RequestMethod.PUT)
    public String updateOrder(Order order) {
        dao.save(order);
        return "redirect:/order";
    }
/*
    @RequestMapping("/aaa")
    public String method1() {
        //Response.getOutputStream.write("method1");
        return "method1";
    }


    @RequestMapping("/bbb")
    public User method2() {
        return new User("aaa", 12, "男", "厦门");
    }


    @RequestMapping("/ccc")
    public User[] method3() {
        return new User[]{new User("aaa", 12, "男", "厦门"), new User("bbb", 12, "男", "厦门")};
    }

    @RequestMapping("/ddd")
    public List<User> method4() {
        return Arrays.asList(new User[]{new User("aaa", 12, "男", "厦门"), new User("bbb", 12, "男", "厦门")});
    }

//   接口
    @RequestMapping("/eee")
    public Map<Integer, User> method5() {
        HashMap<Integer, User> map = new HashMap<>();

        map.put(100, new User("aaa", 12, "男", "厦门"));
        map.put(101, new User("aaa", 12, "男", "厦门"));

        map.put(102, new User("aaa", 12, "男", "厦门"));

        return map;
    }*/
}

